<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1100px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="170px" label-suffix=":" class="form-class">
            <el-form-item label="合同批次号" prop="batch_id">
                <el-input v-model.trim="model.batch_id" placeholder="请输入合同批次号" clearable></el-input>
            </el-form-item>
            <el-form-item label="采购单号" prop="purchase_number">
                <el-input v-model.trim="model.purchase_number" placeholder="请输入合同批次号" clearable></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { getShopList, getSalesmanList } from '@/api/shop'
import { queryAdvplatformList, queryContractDetail, queryContractAdd, queryContractEdit } from '@/api/product'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            model: {
                id: '',
                advertiser_id: '',
                info: '',
                DoYouWantToModifyIt: 0,
                advertiser_name: '',
                admin_id: '',
                shop_id: '',
                platform_id: '',
                status: 1,
                createtime: '',
                updatetime: '',
            },
            rules: {
                advertiser_id: [{ required: true, message: '广告账户不能为空', trigger: ['blur', 'change'] }],
                status: [{ required: true, message: '状态不能为空', trigger: ['blur', 'change'] }],
            },
            dict: {
                platformList: [],
                shopList: [],
                salesmanList: [],
            },
            save: false,
        }
    },
    computed: {
        title() {
            return `${this.type == 'edit' ? '编辑' : '新增'}合同批次号`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async mounted() {
        try {
            // const [platformData, shopData, salesmanData] = await Promise.all([queryAdvplatformList(), getShopList(), getSalesmanList()])
            // console.log(platformData)
            // this.dict.platformList = platformData?.data?.list ?? []
            // this.dict.shopList = shopData?.data?.list ?? []
            // this.dict.salesmanList = salesmanData?.data?.list ?? []
        } catch (error) {
            console.log(error)
        }
        
    },
    methods: {
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            queryContractDetail(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                    // this.model.status = res.status === 'normal' ? 1 : 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }
                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                if (this.type === 'edit') {
                    queryContractEdit(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                } else {
                    queryContractAdd(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                }
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 50%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 250px;
}
.form-class.el-form .el-form-item .el-textarea {
    width: 770px;
}
.form-class.el-form .el-form-item .el-input-number,
.form-class.el-form .el-form-item .el-input-number .el-input {
    width: 120px;
}
.form-class.el-form .el-form-item .el-input-number + .el-input-number {
    margin-left: 10px;
}
.form-class.el-form .el-form-item.full {
    width: 100%;
}
.upload {
}
.upload-tip span {
    color: #ff4949;
    padding: 0 4px;
}
</style>
